<template>
  <div :style="{ width: $store.oj.getWidth }">
    <router-view></router-view>
    <n-watermark v-if="isShow" :content="$store.oj.userData.username" cross fullscreen :font-size="16" :line-height="16"
      :width="$store.oj.systemParams.screenWidth / 3.5" :height="$store.oj.systemParams.screenHeight / 3.5"
      :x-offset="12" :y-offset="60" :rotate="-15" />
  </div>
</template>

<script>
import { userInfoAPI, getNoticeNumAPI } from "@/api/ojAPI/ojAPI";
import { ElNotification } from "element-plus";
import bus from "vue3-eventbus";

export default {
  data() {
    return {
      noticeTime: null,
    };
  },
  created() {
    let firstloading = document.getElementById("firstloading");
    if (firstloading !== null) {
      document.body.removeChild(firstloading);
    }
    bus.on("userNoLogin", () => {
      this.$store.oj.clearUserData();
      this.$router.push({
        path: "/home/homepage",
      });
      this.getCode();
      this.isShowLogining = false;
    });
  },
  mounted() {
    this.getSystemParams();
    this.initUserData();
    window.addEventListener("resize", this.getSystemParams);
  },
  methods: {
    getSystemParams() {
      this.$store.oj.updateScreenParams(window.innerWidth, window.innerHeight);
    },
    initUserData() {
      const token = localStorage.getItem("TOKEN");
      if (token) {
        this.getUserInfo();
      }
    },
    //获取用户信息
    async getUserInfo() {
      await userInfoAPI()
        .then((res) => {
          if (res.data.code === 200) {
            this.$store.oj.updateUserData(res.data.data);
            let permissionList = new Set();
            res.data.data.permissionList?.forEach((item) => {
              if (item.unKey.includes(":super")) {
                permissionList.add(item.unKey.replaceAll(":super", ""));
              } else if (item.unKey.includes(":default")) {
                permissionList.add(item.unKey.replaceAll(":default", ""));
              } else {
                permissionList.add(item.unKey);
              }
            });
            this.$store.oj.updatePermission([...permissionList]);
            this.$store.oj.updateIsLogin(true);
            sessionStorage.setItem("userInfo", JSON.stringify(res.data.data));
            localStorage.setItem("getNotice", true);
            setTimeout(() => {
              this.getNotice();
              bus.emit("getUserTask");
            }, 500);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getNotice() {
      this.getNoticeNum();
      this.noticeTime = setInterval(() => {
        this.getNoticeNum();
      }, 600000);
    },
    async getNoticeNum() {
      if (localStorage.getItem("getNotice")) {
        if (localStorage.getItem("getNotice").includes("true")) {
          await getNoticeNumAPI().then((res) => {
            if (res.data.code === 200) {
              this.$store.oj.discuss.setNoReadNoticeNum(res.data.data);
              if (res.data.data > 0) {
                ElNotification({
                  title: "个人消息",
                  message: `共有${res.data.data}未读`,
                  type: "info",
                  position: "bottom-left",
                });
              }
            }
          });
        } else {
          this.noticeTime = null;
        }
      } else {
        this.noticeTime = null;
      }
    },
  },
  computed: {
    isShow() {
      return String(this.$route.name).includes("raceing");
    },
  },
};
</script>

<style lang="less">
@import url("@/assets/element-ui/index.less");
</style>
